package com.example.compose.retrofit.updataApp

import android.content.Context
import android.content.Intent
import com.example.compose.Constance
import com.example.compose.retrofit.NetWorkManager
import com.example.compose.retrofit.entity.LastUpdate
import com.example.compose.retrofit.entity.Reply
import com.example.compose.utils.LogUtil
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class UpdateApp {

    /**
     * 检查更新
     */
    fun versionUpdate(context: Context){
        NetWorkManager.getRequest()
            .getLastUpdate(Constance.AppInformation.packageName(), "Android")
            .enqueue(object :Callback<Reply<LastUpdate>>{
                override fun onResponse(
                    call: Call<Reply<LastUpdate>>,
                    response: Response<Reply<LastUpdate>>
                ) {
                    val lastUpdate = response.body() ?: Reply<LastUpdate>(0,null)
                    lastUpdate.data?.let { data->
                        val intent = Intent(context, CheckUpdateActivity::class.java)
                        data.app_version_number
                        intent.putExtra("data",data)
                        context.startActivity(intent)
                    }
                }

                override fun onFailure(call: Call<Reply<LastUpdate>>, t: Throwable) {
                    LogUtil.e("检查更新失败${t.message}")
                }
            })
    }

}